package com.hanyao.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanyao.reggie.bean.Category;
import com.hanyao.reggie.bean.Setmeal;
import com.hanyao.reggie.bean.SetmealDish;
import com.hanyao.reggie.common.CustomExcept;
import com.hanyao.reggie.common.R;
import com.hanyao.reggie.dto.SetmealDto;
import com.hanyao.reggie.mapper.SetmealMapper;
import com.hanyao.reggie.service.CategoryService;
import com.hanyao.reggie.service.SetmealDishService;
import com.hanyao.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 寒钥
 * @version 1.0
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
    @Transactional
    @Override
    public void deleteWithDish(List<Long> ids) {
        //删除setmeal
        //判断有没有
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(lambdaQueryWrapper);
        if(count>0){
            throw new CustomExcept("有正在销售的套餐，不能删除");
        }
        this.removeByIds(ids);
        //删除setmealDish
        LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = Wrappers.lambdaQuery();
        setmealDishQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishQueryWrapper);
    }

    @Override
    public SetmealDto showWithDish(Long setmealId) {
        //通过id获取套餐
        Setmeal setmeal = setmealService.getById(setmealId);
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
        //创建套餐与菜品的关连关系表的条件封装器
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //设置条件为套餐id一致
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        //查询在套餐与菜品的关连关系中表满足条件的所有SetmealDish
        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(list);
        setmealDto.setCategoryName(categoryName);
        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改setmeal
        setmealService.updateById(setmealDto);
        //修改setmealDish,1先进行删除，2插入
        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(setmealId != null,SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(lambdaQueryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
